<?php

class get
{
    public static function index():bool
    {
        loadHTML("index",false,["PHPVERSION"=>PHP_VERSION,"SetColor"=>file_get_contents(HTML_PATH."SetColor.html")]);
        return true;
    }

    public static function join():bool
    {
        loadHTML("join");
        return true;
    }

    public static function create():bool
    {
        loadHTML("create");
        return true;
    }

    public static function main():bool
    {
        $roomId = @$_COOKIE["roomId"];
        $pass = @$_COOKIE["roomPassword"];
        $cfg = json_decode(encryptTool::decode(file_get_contents("data/$roomId.cfg"),SECRET,true),true);
        if ($cfg["enablePassword"]=="yes"){
            if ($cfg["password"]!=$pass){
                echo '<html><head><meta charset="UTF-8"><title>密码不正确</title><style>*{margin: 0;padding: 0;}div{background: url("?/file&name=errorPageBackground.gif");opacity: 10%;position: fixed;z-index: -1;width: 100vw;height: 100vh;}p{text-align: center;font-size: 60px;line-height: 10vh;position:relative;top: 45vh;color: red}</style></head><body><div></div><p>密码不正确，请<a href="javascript:history.back();">重试</a>。</p></body></html>';
                die();
            }
        }
        loadHTML("main");
        return true;
    }

    public static function list():bool
    {
        loadHTML("list");
        return true;
    }

    public static function adminCtrl():bool
    {
        loadHTML("adminCtrl");
        return true;
    }

    public static function settings():bool
    {
        loadHTML("settings");
        return true;
    }

    public static function banIP():bool
    {
        loadHTML("banIP");
        return true;
    }

    public static function banWord():bool
    {
        loadHTML("banWord");
        return true;
    }

    public static function visitLog():bool
    {
        loadHTML("visitLog");
        return true;
    }

    public static function clean():bool
    {
        $roomId = $_COOKIE["roomId"];
        $host = (int)explode(".", $_SERVER['REMOTE_ADDR'])[3];
        if ($host > 200) {
            $host -= 200;
        } elseif ($host > 100) {
            $host -= 100;
        }
        $file = DATA_PATH . $roomId . ".room";
        $cfgFile = DATA_PATH . $roomId . ".cfg";
        $cfg = json_decode(encryptTool::decode(file_get_contents($cfgFile),SECRET,true),true);
        if ($cfg["admin"]==$host || post::isAdmin(true)=="yes"){
            $adminFlag = post::isAdmin(true)=="yes"?"超级管理员":"房间管理员";
            file_put_contents($file, encryptTool::encode(json_encode(["<b class=\'system-message\'>${adminFlag}清空了房间的消息</b>"]),SECRET,true));
            file_put_contents(DATA_PATH . $roomId . ".counter", "0");
            header("Location: ./?/main");
        }else{
            echo "<p style='margin-top: 40vh;font-size: 50px;text-align: center;color: red'>你没有权限执行该操作！</p><script>setTimeout(function() {location.href=`./?/main`;},1500);</script>";
        }
        return true;
    }

    public static function file():bool
    {
        if (inPhar()) {
            $f = fopen("phar://index.php/" . $_REQUEST["name"], "r");
        }else{
            $f = fopen("src/files/".$_REQUEST["name"],"r");
        }
        $exp = explode(".",$_REQUEST["name"]);
        $ext = $exp[count($exp)-1];
        $type = getFileType($ext);
        header('Content-Type:'.$type.'; charset=utf-8');
        if ($f) {
            while (!feof($f)) {
                $c = fgetc($f);
                print($c);
            }
        }
        return true;
    }

    public static function file_uploaded():bool
    {
        $fileId = $_REQUEST["id"];
        $filename = "data/files/$fileId";
        if (strlen(file_get_contents($filename))>0) {
            $file = fopen($filename, "r");
            header("Content-type: application/octet-stream");
/*            header("Accept-Ranges: bytes");
            header("Accept-Length: " . filesize($filename));
            header("Content-Disposition: attachment; filename=$filename");
            echo fread($file, filesize($filename));*/
            header("Accept-Ranges: bytes");
            header("Accept-Length: " . filesize($filename));
            header("Content-Disposition: attachment; filename=$fileId");
            while (!feof($file)){
                $c = fgetc($file);
                print($c);
            }
        }else{
            header("HTTP/1.1 404");
            die();
        }
        return true;
    }
}